<template>
  <el-container>
    <el-header>注册登记 </el-header>
    <!-- <el-card>
      <el-row :gutter="10">
        <el-col :span="10">
          <span>注册登记</span>
        </el-col>
        <el-col :span="4">

        </el-col>
      </el-row>
    </el-card> -->
    <el-button type="primary" @click="addDialogVisible = true"
      >添加学籍</el-button
    >
    <el-form ref="form" :model="form" label-width="80px">
      <el-table :data="UserList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="学号" prop="sno"></el-table-column>
        <el-table-column label="姓名" prop="sname"></el-table-column>
        <el-table-column label="性别" prop="ssex"></el-table-column>
        <el-table-column label="学制" prop="ses"></el-table-column>
        <el-table-column label="地区" prop="slocation"></el-table-column>
        <el-table-column label="年龄" prop="sage"></el-table-column>
        <el-table-column label="政治面貌" prop="spo"></el-table-column>
        <el-table-column label="民族" prop="snation"></el-table-column>
        <el-table-column label="奖惩记录" prop="srp"></el-table-column>
        <el-table-column label="休复转退标记" prop="sstm"></el-table-column>
        <el-table-column label="所在专业" prop="smajor"></el-table-column>
        <el-table-column label="年级" prop="syear"></el-table-column>
        <!-- <el-table-column label="状态" prop="state"> -->
        <!-- <template slot-scope="scope">
            <el-switch v-model="scope.row.state"></el-switch>
          </template> -->
        <el-table-column label="删除">
          <template slot-scope="scope">
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteStudent(scope.row.sno)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column label="修改">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.sno)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 插入 -->
      <el-dialog
        title="添加学籍"
        :visible.sync="addDialogVisible"
        width="50%"
        @close="addDialogClosed"
      >
        <el-form
          :model="query"
          label-width="90px"
          :rules="addStudentRules"
          ref="addStudentRef"
        >
          <el-form-item label="学号" prop="sno">
            <el-input v-model="query.sno"></el-input>
          </el-form-item>

          <el-form-item label="姓名" prop="sname">
            <el-input v-model="query.sname"></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="ssex">
            <el-input v-model="query.ssex"></el-input>
          </el-form-item>

          <el-form-item label="学制" prop="ses">
            <el-input v-model="query.ses"></el-input>
          </el-form-item>

          <el-form-item label="地区" prop="slocation">
            <el-input v-model="query.slocation"></el-input>
          </el-form-item>

          <el-form-item label="年龄" prop="sage">
            <el-input v-model="query.sage"></el-input>
          </el-form-item>

          <el-form-item label="政治面貌" prop="spo">
            <el-input v-model="query.spo"></el-input>
          </el-form-item>

          <el-form-item label="民族" prop="snation">
            <el-input v-model="query.snation"></el-input>
          </el-form-item>

          <el-form-item label="奖惩记录" prop="srp">
            <el-input v-model="query.srp"></el-input>
          </el-form-item>

          <el-form-item label="休复转退标记" prop="sstm">
            <el-input v-model="query.sstm"></el-input>
          </el-form-item>

          <el-form-item label="所在专业" prop="smajor">
            <el-input v-model="query.smajor"></el-input>
          </el-form-item>

          <el-form-item label="年级" prop="syear">
            <el-input v-model="query.syear"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addStudent">确定</el-button>
          <el-button type="primary" @click="addDialogVisible = false"
            >取消</el-button
          >
        </span>
      </el-dialog>
      <!-- 编辑 -->
      <el-dialog
        title="编辑学籍"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editDialogClosed"
      >
        <el-form :model="query" label-width="90px" ref="editStudentRef">
          <el-form-item label="学号" prop="sno">
            <el-input v-model="query.sno" disabled></el-input>
          </el-form-item>

          <el-form-item label="姓名" prop="sname">
            <el-input v-model="query.sname"></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="ssex">
            <el-input v-model="query.ssex"></el-input>
          </el-form-item>

          <el-form-item label="学制" prop="ses">
            <el-input v-model="query.ses"></el-input>
          </el-form-item>

          <el-form-item label="地区" prop="slocation">
            <el-input v-model="query.slocation"></el-input>
          </el-form-item>

          <el-form-item label="年龄" prop="sage">
            <el-input v-model="query.sage"></el-input>
          </el-form-item>

          <el-form-item label="政治面貌" prop="spo">
            <el-input v-model="query.spo"></el-input>
          </el-form-item>

          <el-form-item label="民族" prop="snation">
            <el-input v-model="query.snation"></el-input>
          </el-form-item>

          <el-form-item label="奖惩记录" prop="srp">
            <el-input v-model="query.srp"></el-input>
          </el-form-item>

          <el-form-item label="休复转退标记" prop="sstm">
            <el-input v-model="query.sstm"></el-input>
          </el-form-item>

          <el-form-item label="所在专业" prop="smajor">
            <el-input v-model="query.smajor"></el-input>
          </el-form-item>

          <el-form-item label="年级" prop="syear">
            <el-input v-model="query.syear"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="editStudent">确定</el-button>
          <el-button type="primary" @click="editDialogVisible = false"
            >取消</el-button
          >
        </span>
      </el-dialog>
    </el-form>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getUserList();
  },
  data() {
    return {
      form: {},
      query: {
        sno: "",
        sname: "",
        ssex: "",
        ses: "",
        slocation: "",
        sage: "",
        spo: "",
        snation: "",
        srp: "",
        sstm: "",
        smajor: "",
        syear: ""
      },
      addStudentRules: {
        sno: [{ required: true, message: "请输入学号", trigger: "blur" }],
        sname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        ssex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        sses: [{ required: true, message: "请输入学制", trigger: "blur" }],
        slocation: [{ required: true, message: "请输入地区", trigger: "blur" }],
        sage: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        spo: [{ required: true, message: "请输入政治面貌", trigger: "blur" }],
        snation: [{ required: true, message: "请输入民族", trigger: "blur" }],
        srp: [{ required: true, message: "请输入奖惩记录", trigger: "blur" }],
        sstm: [{ required: true, message: "请输入修复转退标记", trigger: "blur" }],
        smajor: [{ required: true, message: "请输入所在专业", trigger: "blur" }],
        syear: [{ required: true, message: "请输入年级", trigger: "blur" }]
      },
      UserList: [],
      total: 0,
      addDialogVisible: false,
      editDialogVisible: false
    }
  },
  methods: {

    //接口调用后端数据
    async getUserList() {
      this.$axios.post("http://localhost:8081/abcd", this.query).then(res => {
        console.log(res.data);
        this.UserList = res.data;
      });
    },
    //关闭窗口
    addDialogClosed() {
      this.$refs.addStudentRef.resetFields();
    },
    //添加学籍操作
    async addStudent() {
      this.$refs.addStudentRef.validate(async valid => {
        if (!valid) return;
        this.$axios.post("http://localhost:8081/addStudent", this.query).then(res => {
          if (res != "success") {
            this.addDialogVisible = false;
            this.getUserList();
            return this.$message.success("操作成功");
          }
          this.$message.error("操作失败");

        });
      });
    },
    //删除学籍操作
    async deleteStudent(sno) {
      const confirmResult = await this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult != 'confirm') {
        return this.$message.info("已取消删除");
      }
      this.$axios.delete("http://localhost:8081/deleteStudent?sno=" + sno).then(res => {
        if (res != "success") {
          this.getUserList();
          return this.$message.success("删除成功");
        }
        this.$message.error("删除失败");
      })
    },

    //修改学籍
    async showEditDialog(sno) {
      this.$axios.get("http://localhost:8081/getupdate?sno=" + sno).then(res => {
        this.query = res.data;
        this.editDialogVisible = true;
      })
    },
    editDialogClosed() {
      this.$refs.editStudentRef.resetFields();
    },
    async editStudent() {
      this.$refs.editStudentRef.validate(async valid => {
        if (!valid) return;
        this.$axios.post("http://localhost:8081/editStudent", this.query).then(res => {
          if (res != "success") {
            this.addDialogVisible = false;
            this.getUserList();
            return this.$message.success("操作成功");
          }
          this.$message.error("操作失败");
        })
      })
    }
  }
};

</script>

<style scoped>
.el-header {
  color: #333;
  line-height: 60px;
  font-size: 40px;
}

.el-form-item {
  font-family: 宋体;
  font-weight: bold;
}

.el-button {
  background-color: #ff4351;
  border-color: #ff4351;
  color: #fff;
}
</style>
